package com.zhouz.admin.app.db.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

@Data
@ToString
@ApiModel
public class Result<T> implements Serializable {
    @ApiModelProperty("编码")
    private Integer code;
    @ApiModelProperty("返回描述")
    private String msg;
    @ApiModelProperty("数据")
    private T data;

    public Result() {
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result();
        result.returnCode(0).msg("操作成功").data(data);
        return result;
    }
    @JsonIgnore
    public  boolean isSuccess() {
        return this.code == 0;
    }
    public static Result success() {
        Result result = new Result();
        result.returnCode(0).msg("操作成功");
        return result;
    }

    public static <T> Result<T> fail() {
        Result<T> result = new Result();
        result.returnCode(502).msg("操作异常");
        return result;
    }

    public static <T> Result<T> fail(Integer code, String msg) {
        Result<T> result = new Result();
        result.returnCode(code).msg(msg);
        result.msg(msg);
        return result;
    }
    public static <T> Result<T> fail(ResCode code) {
        Result<T> result = new Result();
        result.returnCode(code.value()).msg(code.desc());
        return result;
    }
    public static <T> Result<T> fail(String msg) {
        Result<T> result = new Result();
        result.returnCode(502).msg(msg);
        result.msg(msg);
        return result;
    }


    public Result<T> returnCode(Integer errorCode) {
        this.code = errorCode;
        return this;
    }

    public Result<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result<T> data(T data) {
        this.data = data;
        return this;
    }
    public static Result badArgument() {
        return fail(ResCode.BAD_ARGUMENT);
    }

    public static Result badbusy() {
        return fail(ResCode.BAD_BUSY);
    }

    public static Result badArgumentValue() {
        return fail(ResCode.BAD_ARGUMENT_VALUE);
    }

    public static Result validateBad() {
        return fail(ResCode.BAD_VALIDATE);
    }
    public static Result tokenValidError() {
        return fail(ResCode.TOKEN_VALID_ERROR);
    }

    public static Result serious() {
        return fail(ResCode.SERIOUS);
    }
}